<?php
if(!defined('IN_CORE')) exit('permission deny');

// 浏览器友好的变量输出
function D($var, $echo=true, $label=null, $strict=true) {
    $label = ($label === null) ? '' : rtrim($label) . ' ';
    if (!$strict) {
        if (ini_get('html_errors')) {
            $output = print_r($var, true);
            $output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
        } else {
            $output = $label . print_r($var, true);
        }
    } else {
        ob_start();
        var_dump($var);
        $output = ob_get_clean();
        if (!extension_loaded('xdebug')) {
            $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
            $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
        }
    }
    if ($echo) {
        echo($output);
        exit;
        //return null;
    }else
        return $output;
}

function import($filename, $ext='.php') {
	$filename.=$ext;
	if(!file_exists_case($filename)) return NULL;

	return require_cache($filename);
}

// 优化的require_once
function require_cache($filename) {
	static $files = array();
	if (!isset($files[$filename])) {
		if (file_exists_case($filename)) {
			require $filename;
			$files[$filename] = true;
		}
	}
	return $files[$filename];
}

// 区分大小写的文件存在判断
function file_exists_case($filename) {
	if (is_file($filename)) {

		$is_win = strstr(PHP_OS, 'WIN') ? true : false;
		if ($is_win) {
			if (basename(realpath($filename)) != basename($filename))
				return false;
		}
		return true;
	}
	return false;
}

// 配置操作
function C($name=null, $value=null) {
    static $_config = array();
    // 无参数时获取所有
    if (empty($name))
        return $_config;
    // 优先执行设置获取或赋值
    if (is_string($name)) {
        if (!strpos($name, '.')) {
            $name = strtolower($name);
            if (is_null($value))
                return isset($_config[$name]) ? $_config[$name] : null;
            $_config[$name] = $value;
            return;
        }
        // 二维数组设置和获取支持
        $name = explode('.', $name);
        $name[0] = strtolower($name[0]);
        if (is_null($value))
            return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : null;
        $_config[$name[0]][$name[1]] = $value;
        return;
    }
    // 批量设置
    if (is_array($name))
        return $_config = array_merge($_config, array_change_key_case($name));
    return null; // 避免非法参数
}

/**
 * ShowMsg
 *
 * @param string $msg
 * @param string $url
 * @param int $type
 */
function showsMsg($msg,$url='',$limittime=0) {

	$htmlhead  = "<html>\r\n<head>\r\n<title>系统提示</title>\r\n";
	$htmlhead .= "<meta http-equiv=\"Content-Type\" content=\"text/html;\" charset=\"utf-8\"/>\r\n";
	$htmlhead .= "<base target='_self'/>\r\n</head>\r\n";
	$htmlhead .= "<body leftmargin='0' topmargin='0'>\r\n<center>\r\n<script>\r\n";

	$htmlfoot  = "</script>\r\n</center>\r\n</body>\r\n</html>\r\n";

	if($limittime==0) $litime = 3000;
	else $litime = $limittime;

	if($url=="-1"){
		if($limittime==0) $litime = 3000;
		$url = "javascript:history.go(-1);";
	}

	if($url==""){
		$msg = "<script>alert(\"".str_replace("\"","“",$msg)."\");</script>";
	}else{
		$func = "var pgo=0;
		function JumpUrl(){
		if(pgo==0){ location='$url'; pgo=1; }
	}\r\n";

	$rmsg = $func;
	$rmsg .= "document.write(\"<br/>";
	$rmsg .= "<div style='width:400px;padding-top:4px;height:24;font-size:10pt;border-left:1px solid #b9df92;border-top:1px solid #b9df92;border-right:1px solid #b9df92;background-color:#def5c2;'>提示信息：</div>\");\r\n";
	$rmsg .= "document.write(\"<div style='width:400px;height:100;font-size:10pt;border:1px solid #b9df92;background-color:#f9fcf3'><br/><br/>\");\r\n";
	$rmsg .= "document.write(\"".str_replace("\"","“",$msg)."\");\r\n";
	$rmsg .= "document.write(\"";

	//if($onlymsg==0){
	if($url!="javascript:;" && $url!=""){
	$rmsg .= "<br/><br/><a href='".$url."'>如果你的浏览器没反应，请点击这里...</a>";
	}
	$rmsg .= "<br/><br/></div>\");\r\n";
	if($url!="javascript:;" && $url!=""){
	$rmsg .= "setTimeout('JumpUrl()',$litime);";
	}
	//}else{ $rmsg .= "<br/><br/></div>\");\r\n"; }
	$msg  = $htmlhead.$rmsg.$htmlfoot;
	}

	echo $msg;
	exit();
}

function pager($total, $limit=10) {
	$pag = intval($_GET['pag']) ? intval($_GET['pag']) : 1;
	$range = 10;
	$pageNum = ceil($total/$limit);
	$pager = '';
	$pager .= "<a href='?pag=1'>[1]</a>";
	
	if($pag > $range) $start = $pag - $range;
	else $start = $pag;
	if($pag + $range <= $pageNum)
		$end = $pag + $range;
	else $end = $pageNum;
	
	for($step = $start; $step < $end; $step++) {
		if($step>1) $pager .= "<a href='?pag={$step}'>[{$step}]</a>";
	}
	$pager .= "<a href='?pag={$pageNum}'>[{$pageNum}]</a>";
	return $pager;
}

/**
 * phpPath
 *
 * @return string 当前php的路径
 */
function phpPath() {
	return './'.basename($_SERVER['PHP_SELF']);
}


/**
 * tempPath
 *
 * @param string $act 当前动作
 * @param string $tempdir 模板目录
 * @param int $tempExtension 模板后缀
 */
function tempPath($act = '', $tempdir = 'templats', $tempExtension = 'html') {
	if(!empty($act)) $act = '_'.$act;
	return './'.$tempdir.'/'.basename(phpPath(),".php").$act.'.'.$tempExtension; 
}